import numpy as np
import cv2
def CenterLabelHeatMap(img_width, img_height, c_x, c_y,sigma):
    Y1 = np.linspace(0, img_height-1, img_height)
    X1 = np.linspace(0, img_width-1, img_width)
    [X, Y] = np.meshgrid(X1, Y1)
    X = X - c_x
    Y = Y - c_y
    D2 = X * X + Y * Y
    E2 = 2. * sigma * sigma
    Exponet = D2 / E2
    heatmap = np.exp(-Exponet)
    return heatmap

def random_rotate(img,keypoints):
    from random import randint
    angle = randint(-30,30)
    matrix = cv2.getRotationMatrix2D((img.shape[1]//2.,img.shape[0]/2.),angle,1.0);
    img = cv2.warpAffine(img,matrix,(img.shape[1],img.shape[0]))
    keypoints_r = []
    for x,y in keypoints:
        p0 = np.array([x,y]).astype(np.float32).reshape(1,1,2)
        p = cv2.transform(p0,matrix).reshape(1,-1)
        x = p[0,0]
        y = p[0,1]
        keypoints_r.append([x,y])
    return img,np.array(keypoints_r)